package com.criticalritrpg.bd;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import com.criticalritrpg.R;

public class BDService extends SQLiteOpenHelper {
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	
	private static final String DB_NAME = "mestre";
	private static final int DB_VERSION = 1;
	//
	public static final String TABLE_NAME_JOGOS = "jogos";
	public static final String JOGO_ID = "_id";
	public static final String COL_ID_JOGO = " (_id INTEGER PRIMARY KEY AUTOINCREMENT, ";
	public static final String COL_MESTRE = "nMestre";
	public static final String COL_DATE_INICIO = "pDate";
	
	//
	public static final String COL_NAME_PARTICIPANTE = "nPartipantes";
	public static final String COL_EMAIL_PARTICIPANTE = "ePartipantes";
	//public static final String TABLE_NAME_PARTICIPANTES = "partipantes";
	public static final String FK_JOGOID = "jogoid";
	//private static final String COL_FKEY_JOGO_JOGADOR = "jogoid INTERGER, "+"FOREIGN KEY("+FK_JOGOID+") "+
		//	"REFERENCES "+ TABLE_NAME_JOGOS+"("+JOGO_ID+"), ";
	//public static final String COL_ID_PARTICIPANTE = " (_id INTEGER PRIMARY KEY AUTOINCREMENT, ";
	
	//
	public static final String TABLE_NAME_AGENDA = "agenda";
	//public static final String ID_AGENDA = "agenda_id";
	private static final String COL_FKEY_JOGO_AGENDA = FK_JOGOID+" INTERGER, "+"FOREIGN KEY("+FK_JOGOID+") "+
			"REFERENCES "+ TABLE_NAME_JOGOS+"("+JOGO_ID+")";
	public static final String COL_ID_AGENDA = " (_id INTEGER PRIMARY KEY AUTOINCREMENT, ";
	public static final String COL_DATE_SESSAO = "sDate";
	public static final String COL_LOCAL_SESSAO = "sLocal";
	
	//
	private static final String STRING_CREATE_JOGO = 
			"CREATE TABLE "+TABLE_NAME_JOGOS+COL_ID_JOGO
					+COL_MESTRE+" TEXT, "+COL_DATE_INICIO+" DATE);";
	//
	
	//
	//private static final String STRING_CREATE_PATICIPANTES = 
		//	"CREATE TABLE "+TABLE_NAME_PARTICIPANTES+COL_ID_PARTICIPANTE
			//		+COL_NAME_PARTICIPANTE+" TEXT, "+COL_EMAIL_PARTICIPANTE+" TEXT);";
	//
	private static final String STRING_CREATE_AGENDA = 
			"CREATE TABLE "+TABLE_NAME_AGENDA+COL_ID_AGENDA
			+COL_DATE_SESSAO+" DATE, "+COL_LOCAL_SESSAO+" TEXT, "+COL_FKEY_JOGO_AGENDA+");";
	
	public BDService(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
		
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		db.execSQL(STRING_CREATE_JOGO);
		//db.execSQL(STRING_CREATE_PATICIPANTES);
		db.execSQL(STRING_CREATE_AGENDA);
		Log.d("agenda", STRING_CREATE_AGENDA);
		//You may also load initial values into the database here
		//ContentValues cv = new ContentValues(2);
		//cv.put(COL_MESTRE, "mestre");
		//Create a formatter for SQL date format
		//SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		//cv.put(COL_DATE_INICIO, dateFormat.format(new java.util.Date())); //Insert 'now' as the date
		//db.insert(TABLE_NAME_JOGOS, null, cv);
		
		
		
		//cv = new ContentValues(3);
		//cv.put(FK_JOGOID, 1);
		//cv.put(COL_NAME_PARTICIPANTE, "jogador");
		//cv.put(COL_EMAIL_PARTICIPANTE, "hhfd@gmail.com"); //Insert 'now' as the date
		//db.insert(TABLE_NAME_PARTICIPANTES, null, cv);
		
		//cv = new ContentValues(2);
		//cv.put(COL_DATE_SESSAO, dateFormat.format(new java.util.Date()));
		//cv.put(FK_JOGOID, 1);
		//db.insert(TABLE_NAME_AGENDA, null, cv);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		//For now, clear the database and re-create
		db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME_JOGOS);
		//db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME_PARTICIPANTES);
		db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME_AGENDA);
		onCreate(db);
		
	}
	
	public void insereMestre(String nome) {
		ContentValues cv = new ContentValues(2);
		cv.put(COL_MESTRE, nome);
		cv.put(COL_DATE_INICIO, dateFormat.format(new Date()));
		getWritableDatabase().insert(TABLE_NAME_JOGOS,null ,cv );
		
	}
	
	public void insereAgendaData(Date d, int id_jogo, String local) {
		
		ContentValues cv = new ContentValues(3);
		cv.put(FK_JOGOID, id_jogo);
		cv.put(COL_DATE_SESSAO, dateFormat.format(d));
		cv.put(COL_LOCAL_SESSAO,local);
		getWritableDatabase().insert(TABLE_NAME_AGENDA, null, cv);
		
	}
	
	
	public void insereDatainicial(Date d, int id_jogo) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		ContentValues cv = new ContentValues(1);
		cv.put(COL_DATE_INICIO, dateFormat.format(d));
		getWritableDatabase().update(TABLE_NAME_JOGOS, cv, JOGO_ID+"=?",new String []{String.valueOf(id_jogo)} );
		
	}
	
	
	
	
	
}
